{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "# 随机数\n",
    "- np.random 是numpy中提供的随机数\n",
    "- random Python内置的随机数"
   ],
   "id": "52db6bd011fa764c"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "## 1. seed 向随机数生成器传递随机状态种子\n",
    "- 只要random.seed( * ) seed里面的值一样，那随机出来的结果就一样。所以说，seed的作用是让随机结果可重现。也就是说当我们设置相同的seed，每次生成的随机数相同。如果不设置seed，则每次会生成不同的随机数。使用同一个种子，每次生成的随机数序列都是相同的。\n"
   ],
   "id": "6db0e2cacf156e88"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-28T07:42:39.698682Z",
     "start_time": "2025-08-28T07:42:39.696146Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "from numpy import random\n",
    "from numpy.ma.extras import average"
   ],
   "id": "cdee5c1f820bd4ca",
   "outputs": [],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-28T07:44:23.633513Z",
     "start_time": "2025-08-28T07:44:23.630177Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "random.seed(0)\n",
    "# random()生成 0-1之间的随机数\n",
    "print(random.random())\n",
    "print(random.random())\n",
    "print(random.random())\n",
    "print(random.random())\n",
    "print(random.random())"
   ],
   "id": "47ae86b5e92bd8e2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.5488135039273248\n",
      "0.7151893663724195\n",
      "0.6027633760716439\n",
      "0.5448831829968969\n",
      "0.4236547993389047\n"
     ]
    }
   ],
   "execution_count": 45
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "## 2. rand 返回[0,1]之间，从均匀分布中抽取样本\n",
    "- 均匀分布表示在某个区间内，所有点的出现概率都相等。分为两种类型：\n",
    "    - 离散均匀分布\n",
    "        - 每个可能的结果有相同的概率\n",
    "        - 例子：掷骰子，每个面概率都是1/6\n",
    "    - 连续均匀分布\n",
    "        - 在区间 [a, b] 内，任何点的概率密度相同"
   ],
   "id": "62dc3d3190bec2eb"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-28T07:52:02.484219Z",
     "start_time": "2025-08-28T07:52:02.479800Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr1 = random.rand(3)\n",
    "print(arr1)\n",
    "print('=' * 30)\n",
    "\n",
    "arr2 = random.rand(2,3)\n",
    "print(arr2)\n",
    "\n",
    "print('=' * 30)\n",
    "\n",
    "arr3 = random.rand(2,3,4)\n",
    "print(arr3)\n"
   ],
   "id": "4f095fbba0acb612",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.77815675 0.87001215 0.97861834]\n",
      "==============================\n",
      "[[0.79915856 0.46147936 0.78052918]\n",
      " [0.11827443 0.63992102 0.14335329]]\n",
      "==============================\n",
      "[[[0.94466892 0.52184832 0.41466194 0.26455561]\n",
      "  [0.77423369 0.45615033 0.56843395 0.0187898 ]\n",
      "  [0.6176355  0.61209572 0.616934   0.94374808]]\n",
      "\n",
      " [[0.6818203  0.3595079  0.43703195 0.6976312 ]\n",
      "  [0.06022547 0.66676672 0.67063787 0.21038256]\n",
      "  [0.1289263  0.31542835 0.36371077 0.57019677]]]\n"
     ]
    }
   ],
   "execution_count": 49
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "## 3. randn 返回标准正态分布随机数（浮点数）平均数0，方差1 【了解】\n",
    "- randn生成一个从标准正态分布中得到的随机标量，标准正态分布即N(0，1)。\n",
    "- randn（n)和randn(a1,a2,...)的用法和rand相同、\n",
    "- 方差： 数组中每一个元素数 和 数组平均数的平方 的差\n",
    "- 标准差，方差的平方根\n"
   ],
   "id": "1aa016466f7ce25f"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-28T08:02:59.162823Z",
     "start_time": "2025-08-28T08:02:59.157295Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr4 = random.randn(3)\n",
    "print(arr4)\n",
    "print('=' * 30)\n",
    "\n",
    "arr5 = random.randn(2,3)\n",
    "print(arr5)\n",
    "print('=' * 30)\n",
    "\n",
    "arr6 = random.randn(2,3,4)\n",
    "print(arr6)"
   ],
   "id": "2c3bf480696e2aae",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1.34395442 -0.81822096  0.08270994]\n",
      "==============================\n",
      "[[-1.29105846 -0.66110423 -1.18019102]\n",
      " [ 0.19764264  0.41389998  1.19732198]]\n",
      "==============================\n",
      "[[[ 1.88335386  0.71422382  2.28433335  1.56410258]\n",
      "  [ 0.61110371 -0.87736332 -1.6210875  -0.581673  ]\n",
      "  [-0.53783395 -1.55602368 -0.05446484 -1.81127879]]\n",
      "\n",
      " [[-0.63117522 -0.92815918  1.49072192  0.19549934]\n",
      "  [-0.47160433  1.81235465 -2.29413748  0.65120935]\n",
      "  [-1.13049645 -0.77734666  1.11593845  1.33945302]]]\n"
     ]
    }
   ],
   "execution_count": 54
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 4. randint 随机整数",
   "id": "3d487c700da77fad"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-28T08:24:39.714615Z",
     "start_time": "2025-08-28T08:24:39.710148Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a1 = random.randint(3)\n",
    "print(f'随机生成0-3之间的随机数{a1}')\n",
    "a2 = random.randint(1,10)\n",
    "print(f'生成1到10之间的随机数，{a2}')\n",
    "\n",
    "arr7 = random.randint(1,10,size=(5,))\n",
    "print(f'随机生成1-10之间的元素，组成一个长度为5的一维数组{arr7}')\n",
    "\n",
    "arr8 = random.randint(1,20,size=(3,4))\n",
    "print(f'随机生成1-20之间的元素，组成一个三行四列 的二维数组 \\n {arr8}')\n",
    "\n",
    "arr9 = random.randint(1,20,size=(3,4,5))\n",
    "print(f'随机生成1-20之间的元素，组成一个三页四行五列 的三维数组 \\n {arr9}')"
   ],
   "id": "8725e78a7e9d425c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "随机生成0-3之间的随机数2\n",
      "生成1到10之间的随机数，4\n",
      "随机生成1-10之间的元素，组成一个长度为5的一维数组[3 6 5 2 6]\n",
      "随机生成1-20之间的元素，组成一个三行四列 的二维数组 \n",
      " [[ 4  6  9  5]\n",
      " [18  2 19  2]\n",
      " [15 18  8 12]]\n",
      "随机生成1-20之间的元素，组成一个三页四行五列 的三维数组 \n",
      " [[[15 17  2 18 13]\n",
      "  [15  1  3 15 19]\n",
      "  [12 10 15  1 13]\n",
      "  [10 19  5  8  4]]\n",
      "\n",
      " [[17 13  6 14 17]\n",
      "  [14 19 18  8 18]\n",
      "  [15  4 11 11  2]\n",
      "  [17 11 19 17  3]]\n",
      "\n",
      " [[ 7 10  3 13 10]\n",
      "  [ 5  5 10 11 13]\n",
      "  [ 9  2  6  8  1]\n",
      "  [ 2  4 19 19  9]]]\n"
     ]
    }
   ],
   "execution_count": 304
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 5. random 生成0.0至1.0的随机数",
   "id": "c3c8cc160a28b908"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-28T08:28:30.331713Z",
     "start_time": "2025-08-28T08:28:30.327624Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr10 = random.random(3)\n",
    "print(f'生成3个0.0-1.0之间的随机一维数组{arr10}')\n",
    "\n",
    "arr11 = random.random(size=(2,3))\n",
    "print(f'生成3个0.0-1.0之间的随机2行3列的二维数组 \\n {arr11}')\n",
    "\n",
    "arr12 = random.random(size=(2,3,4))\n",
    "print(f'生成3个0.0-1.0之间的随机2页3行4列的二维数组 \\n {arr12}')"
   ],
   "id": "8d00018fffe645aa",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "生成3个0.0-1.0之间的随机一维数组[0.09941639 0.28977588 0.53907355]\n",
      "生成3个0.0-1.0之间的随机2行3列的二维数组 \n",
      " [[0.72414785 0.38886743 0.22708397]\n",
      " [0.45485988 0.97208251 0.83381823]]\n",
      "生成3个0.0-1.0之间的随机2页3行4列的二维数组 \n",
      " [[[0.91479066 0.66728484 0.44066609 0.68509199]\n",
      "  [0.64859831 0.02910001 0.91953144 0.542457  ]\n",
      "  [0.99114165 0.38310289 0.80666933 0.46100683]]\n",
      "\n",
      " [[0.82682421 0.53911823 0.88726484 0.88010686]\n",
      "  [0.32830331 0.40442494 0.54464736 0.6216929 ]\n",
      "  [0.37935608 0.54212945 0.12091936 0.68069724]]]\n"
     ]
    }
   ],
   "execution_count": 312
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 6. choice 从一维数组中生成随机数",
   "id": "f528a932c7f5a8b8"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-28T08:30:14.198666Z",
     "start_time": "2025-08-28T08:30:14.193627Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 第一参数是一个1维数组，如果只有一个数字那就看成range(5)\n",
    "# 第二参数是维度和元素个数，一个数字是1维，数字是几就是几个元素\n",
    "a = np.random.choice(5,3)\n",
    "print(f'从range(5)中拿随机数，生成只有3个元素的一维数组是：{a}')\n"
   ],
   "id": "a014b0d82384343",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "从range(5)中拿随机数，生成只有3个元素的一维数组是：[1 4 4]\n"
     ]
    }
   ],
   "execution_count": 313
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-28T08:36:05.624954Z",
     "start_time": "2025-08-28T08:36:05.617151Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "arr13 = np.arange(10)\n",
    "print(arr13)\n",
    "print('=' * 30)\n",
    "\n",
    "arr14 = random.choice(arr13,6)\n",
    "print('一维数组',arr14)\n",
    "\n",
    "arr15 = random.choice(arr13,(2,3))\n",
    "print(f'二维数组:\\n {arr15}')\n",
    "\n",
    "\n",
    "arr16 = random.choice(arr13,(2,3,4))\n",
    "print(f'三维数组:\\n {arr16}')\n",
    "\n"
   ],
   "id": "7cedffa76d39b586",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4 5 6 7 8 9]\n",
      "==============================\n",
      "一维数组 [9 9 0 4 7 3]\n",
      "二维数组:\n",
      " [[2 7 2]\n",
      " [0 0 4]]\n",
      "三维数组:\n",
      " [[[5 5 6 8]\n",
      "  [4 1 4 9]\n",
      "  [8 1 1 7]]\n",
      "\n",
      " [[9 9 3 6]\n",
      "  [7 2 0 3]\n",
      "  [5 9 4 4]]]\n"
     ]
    }
   ],
   "execution_count": 333
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 7. shuffle(数组)把一个数进行随机排列(洗牌)",
   "id": "12504c0029674c90"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-28T08:38:44.178248Z",
     "start_time": "2025-08-28T08:38:44.173338Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr17 = np.arange(10)\n",
    "print('排序前',arr17)\n",
    "# 乱序\n",
    "random.shuffle(arr17)\n",
    "\n",
    "print('排序后:',arr17)\n"
   ],
   "id": "655be9624f510bf4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "排序前 [0 1 2 3 4 5 6 7 8 9]\n",
      "排序后: [1 2 5 0 9 3 8 7 4 6]\n"
     ]
    }
   ],
   "execution_count": 335
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-28T08:41:39.652889Z",
     "start_time": "2025-08-28T08:41:39.649162Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr18 = np.arange(21).reshape(3,7)\n",
    "print('乱序前',arr18)\n",
    "\n",
    "# 乱序 多维数组随机排列只按行随机，列是不变的\n",
    "random.shuffle(arr18)\n",
    "\n",
    "print('乱序后:\\n',arr18)"
   ],
   "id": "a81fb8a3126dfd11",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "排序前 [[ 0  1  2  3  4  5  6]\n",
      " [ 7  8  9 10 11 12 13]\n",
      " [14 15 16 17 18 19 20]]\n",
      "排序后:\n",
      " [[14 15 16 17 18 19 20]\n",
      " [ 7  8  9 10 11 12 13]\n",
      " [ 0  1  2  3  4  5  6]]\n"
     ]
    }
   ],
   "execution_count": 339
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-28T08:45:39.225451Z",
     "start_time": "2025-08-28T08:45:39.221603Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr19 = np.arange(24).reshape(3,2,4)\n",
    "print('乱序前\\n',arr19)\n",
    "\n",
    "# 乱序， 只对页排序，行列不变\n",
    "random.shuffle(arr19)\n",
    "\n",
    "print('乱序后:\\n',arr19)\n"
   ],
   "id": "e9faa687fda60c09",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "排序前\n",
      " [[[ 0  1  2  3]\n",
      "  [ 4  5  6  7]]\n",
      "\n",
      " [[ 8  9 10 11]\n",
      "  [12 13 14 15]]\n",
      "\n",
      " [[16 17 18 19]\n",
      "  [20 21 22 23]]]\n",
      "<UNK>:\n",
      " [[[16 17 18 19]\n",
      "  [20 21 22 23]]\n",
      "\n",
      " [[ 0  1  2  3]\n",
      "  [ 4  5  6  7]]\n",
      "\n",
      " [[ 8  9 10 11]\n",
      "  [12 13 14 15]]]\n"
     ]
    }
   ],
   "execution_count": 346
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 8. permutation(数组) 把一个数组随机排列或者数字全排列",
   "id": "11bb07cd8639c479"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-28T08:47:37.935740Z",
     "start_time": "2025-08-28T08:47:37.930450Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 与上面讲的np.random.shuffle(一维数组)效果一样,就是把一维数组重新排序了\n",
    "arr20 = random.permutation(10)    # 这里的10就看成是range(10)\n",
    "print(arr20)"
   ],
   "id": "65bf0cf31e6b141e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[6 4 2 7 8 0 3 9 5 1]\n"
     ]
    }
   ],
   "execution_count": 347
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-28T08:50:17.533890Z",
     "start_time": "2025-08-28T08:50:17.530033Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr21 = np.arange(21).reshape(3,7)\n",
    "print('乱序前\\n',arr21)\n",
    "\n",
    "# 只排行，不排列\n",
    "arr22 = random.permutation(arr21)\n",
    "print('乱序后:\\n',arr22)"
   ],
   "id": "4d25ecd620f3ff13",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "排序前\n",
      " [[ 0  1  2  3  4  5  6]\n",
      " [ 7  8  9 10 11 12 13]\n",
      " [14 15 16 17 18 19 20]]\n",
      "乱序后:\n",
      " [[14 15 16 17 18 19 20]\n",
      " [ 7  8  9 10 11 12 13]\n",
      " [ 0  1  2  3  4  5  6]]\n"
     ]
    }
   ],
   "execution_count": 383
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "## 9. normal 生成正态分布数字\n",
    "-  正态分布，又叫常态分布，又叫高斯分布\n",
    "- 语法：normal[平均值，方差，size]\n",
    "- 相关函数：\n",
    "    - mean()  求平均值\n",
    "    - std() 标准差\n",
    "    - var() 方差\n",
    "    - average() 加权平均值\n",
    "\n"
   ],
   "id": "ab7da9be0f86f71d"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-28T09:02:02.079554Z",
     "start_time": "2025-08-28T09:02:02.073540Z"
    }
   },
   "cell_type": "code",
   "source": [
    "random.seed(3)\n",
    "arr23 = random.normal(1,10,10)\n",
    "print('生成一个平均值是1，标准差是10，长度是10的一维数组：\\n',arr23)\n",
    "\n",
    "print('数组平均值是：',np.mean(arr23))\n",
    "print('数组的方差是:',np.var(arr23))\n",
    "print('数组的标准差是:',np.std(arr23))\n",
    "print('数组的长度是:',len(arr23))\n",
    "np.average(arr23)\n",
    "\n",
    "\n"
   ],
   "id": "d9a5f264f608dd51",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "生成一个平均值是1，标准差是10，长度是10的一维数组：\n",
      " [ 18.88628473   5.36509851   1.96497468 -17.63492703  -1.77388203\n",
      "  -2.54758979   0.17258519  -5.27000677   0.56181831  -3.7721803 ]\n",
      "数组平均值是： -0.4047824507752981\n",
      "数组的方差是: 75.06739339190275\n",
      "数组的标准差是: 8.664144123449399\n",
      "数组的长度是: 10\n"
     ]
    }
   ],
   "execution_count": 399
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 10 uniform 均匀分布",
   "id": "4d50cf37a26577d3"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-28T09:14:43.185363Z",
     "start_time": "2025-08-28T09:14:43.180760Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr24  = random.uniform(1,10,10)\n",
    "print(f'在1到10之间生成10个随机数：\\n{arr24}')"
   ],
   "id": "a5b809fca0d473fc",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "在1到10之间生成10个随机数：\n",
      "[6.84229643 3.50638554 7.08629412 6.31776536 1.21583694 6.02968679\n",
      " 3.33327202 4.73591077 3.55172574 7.23824126]\n"
     ]
    }
   ],
   "execution_count": 400
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-28T09:15:59.057218Z",
     "start_time": "2025-08-28T09:15:59.053943Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr25 = random.uniform(1,10,(2,3))\n",
    "print(f'在1到10之间生成2行3列共计6个随机数：\\n{arr25}')\n"
   ],
   "id": "8b7289bf64686178",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "在1到10之间生成2行3列共计6个随机数：\n",
      "[[4.96408346 2.41180965 5.90184116]\n",
      " [8.02283288 3.75727179 2.99762096]]\n"
     ]
    }
   ],
   "execution_count": 401
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
